package com.itmk.web.sys_student.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.sys_student.entity.PageParm;
import com.itmk.web.sys_student.entity.SysStudent;
import com.itmk.web.sys_student.service.SysStudentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-09
 */
@RestController
@RequestMapping("/api/student")
public class SysStudentController {
    @Autowired
    private SysStudentService sysStudentService;

    //重置密码
    @PostMapping("/resetPassword")
    public ResultVo resetPassword(@RequestBody SysStudent sysStudent){
        sysStudent.setPassword("123456");
        if(sysStudentService.updateById(sysStudent)){
            return ResultUtils.success("重置成功");
        }
        return ResultUtils.error("重置失败!");
    }
    //新增
    @PostMapping
    public ResultVo add(@RequestBody SysStudent sysStudent){
        //判断学号是否重复
        QueryWrapper<SysStudent> query = new QueryWrapper<>();
        query.lambda().eq(SysStudent::getClassId,sysStudent.getClassId())
                .eq(SysStudent::getStuNum,sysStudent.getStuNum());
        SysStudent one = sysStudentService.getOne(query);
        if(one != null){
            return ResultUtils.error("学号重复,请录入其他学号!");
        }
        if(sysStudentService.save(sysStudent)){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody SysStudent sysStudent){
        //判断学号是否重复
        QueryWrapper<SysStudent> querys = new QueryWrapper<>();
        querys.lambda().eq(SysStudent::getClassId,sysStudent.getClassId())
                .eq(SysStudent::getStuNum,sysStudent.getStuNum());
        SysStudent one = sysStudentService.getOne(querys);
        if(one != null){
            return ResultUtils.error("学号重复,请录入其他学号!");
        }
        if(sysStudentService.updateById(sysStudent)){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败!");
    }

    //删除
    @DeleteMapping("/{stuId}")
    public ResultVo delete(@PathVariable("stuId") Integer stuId){
        if(sysStudentService.removeById(stuId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public ResultVo getList(PageParm parm){
        //构造分页对象
        IPage<SysStudent> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<SysStudent> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getPhone()),SysStudent::getPhone,parm.getPhone())
                .like(StringUtils.isNotEmpty(parm.getStuName()),SysStudent::getStuName,parm.getStuName())
                .eq(StringUtils.isNotEmpty(parm.getClassId()),SysStudent::getClassId,parm.getClassId())
                .eq(StringUtils.isNotEmpty(parm.getStuNum()),SysStudent::getStuNum,parm.getStuNum());
        IPage<SysStudent> list = sysStudentService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }
}

